package com.example.service.impl;

import com.example.entity.Staffs;
import com.example.entity.Village;
import com.example.mapper.StaffsMapper;
import com.example.mapper.VillageMapper;
import com.example.result.CommonResult;
import com.example.service.VillageSerivce;
import com.example.utils.MyOSSUtil;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * @project: smart-property
 * @description: 小区信息管理 service 实现层
 * @author: cjq
 * @create: 2022-11-15 10:52
 **/
@Service
public class VillageSerivceImpl implements VillageSerivce {
    @Autowired
    VillageMapper villageMapper;

    @Autowired
    StaffsMapper staffsMapper;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 判断小区编号是否重复
     * @param villageNumber
     * @return
     */
    @Override
    public CommonResult listVillageNumber(String villageNumber) {
        Village village = villageMapper.listVillageNumber(villageNumber);
        if (!StringUtils.isEmpty(village)){
            return CommonResult.fail("小区编号已存在，如有疑问请联系管理员!");
        }
        return CommonResult.ok(village);
    }

    /**
     * 添加/修改小区信息
     * @param file
     * @param village
     * @param staffs
     * @return
     */
    @Override
    public int saveVillageInfo(MultipartFile[] file, Village village, Staffs staffs) {
        int i=0;
        if (file != null){
            MyOSSUtil myOSSUtil = new MyOSSUtil();
            String[] s = new String[file.length];
            for (int a=0;a<s.length;a++){
                s[a] = myOSSUtil.uploadFile(file[a]);
            }
            String image = Arrays.toString(s).replaceAll("\\[", "").replaceAll("\\]", "");
            if (village.getId() == null){
                village.setVillageImage(image);
                village.setDelFlag(0);
                i = villageMapper.addVillageInfo(village);
                staffs.setId(village.getHId());
                staffsMapper.updateStaffsInfo(staffs);
                redisUtil.del("villageInfo");
            }else {
                village.setVillageImage(image);
                village.setDelFlag(0);
                i = villageMapper.updateVillageInfo(village);
                staffs.setId(village.getHId());
                staffsMapper.updateStaffsInfo(staffs);
                redisUtil.del("villageInfo");
            }
        }else {
            if (village.getId() == null){
                village.setDelFlag(0);
                i = villageMapper.addVillageInfo(village);
                staffs.setId(village.getHId());
                staffsMapper.updateStaffsInfo(staffs);
                redisUtil.del("villageInfo");
            }else {
                village.setDelFlag(0);
                i = villageMapper.updateVillageInfo(village);
                staffs.setId(village.getHId());
                staffsMapper.updateStaffsInfo(staffs);
                redisUtil.del("villageInfo");
            }
        }
        return i;
    }

    /**
     * 小区信息列表展示
     * @return
     */
    @Override
    public List<Village> queryVillageInfo() {
        return villageMapper.queryVillageInfo();
    }

    /**
     * 删除小区信息
     * @param id
     * @return
     */
    @Override
    public int delVillageInfo(Integer id) {
        redisUtil.del("villageInfo");
        return villageMapper.delVillageInfo(id);
    }
}
